/**
 * Copyright (Warren)
 * FileName: _2_twoSum
 * Author:   warren
 * Date:     2019/3/8 0:23
 * Description:
 * History:
 */
package leetcode;

import java.util.HashMap;
import java.util.Map;

public class _2_twoSum {
    class Solution {
        /**
         method1: O(n^2)
         method1 begin:

        public int[] twoSum(int[] nums, int target){
            int[] res = {0,0};
            for (int i = 0; i < nums.length-1; i++)
                for (int j = i+1; j < nums.length; j++){
                    if (nums[i] + nums[j] == target){
                        res[0] = i;
                        res[1] = j;
                    }
                }
            return res;
        }

         *method end
         */
        public int[] twoSum(int[] nums, int target){
            Map<Integer, Integer> map = new HashMap<Integer, Integer>();
            for (int i = 0; i < nums.length; i++){
                int complement = target - nums[i];
                if (map.containsKey(complement))
                    return new int[] {map.get(complement), i};
                else
                    map.put(nums[i],i);
            }
            throw new IllegalArgumentException("No two sum solution");
        }
    }

    public Solution getSolution(){
        return new Solution();
    }

    public static void main(String[] args){
        _2_twoSum c = new _2_twoSum();
        _2_twoSum.Solution s = c.getSolution();
        int[] nums = {-1, 0, 1, 2, -1, -4};
        int target = 3;
        int[] res = s.twoSum(nums, target);
        System.out.println("res0:"+res[0]);
        System.out.println("res1:"+res[1]);
    }
}
